#!/bin/bash

# 获取当前时间戳
timestamp=$(date +%s)

# 收集系统性能数据
collect_perf_data() {
    # 1. 使用 top 收集 CPU 和进程信息
    top_data=$(top -b -n 1)
    
    # 2. 使用 free 收集内存信息
    mem_data=$(free -m | awk 'NR==2')
    swap_data=$(free -m | awk 'NR==3')
    
    # 3. 使用 iotop 收集磁盘 I/O 信息（改进版）
    # 获取总磁盘 I/O 数据
    iotop_output=$(sudo iotop -b -n 2 -k -o -qqq 2>/dev/null)
    
    # 提取总磁盘读写数据
    total_disk_line=$(echo "$iotop_output" | grep "Total DISK" | head -1)
    
    if [ -n "$total_disk_line" ]; then
        # 提取读速率
        read_part=$(echo "$total_disk_line" | awk -F 'Total DISK READ :' '{print $2}' | awk -F '|' '{print $1}')
        read_kbs=$(echo "$read_part" | sed 's/[^0-9.]//g')
        
        # 提取写速率
        write_part=$(echo "$total_disk_line" | awk -F 'Total DISK WRITE :' '{print $2}')
        write_kbs=$(echo "$write_part" | sed 's/[^0-9.]//g')
    else
        read_kbs="0.00"
        write_kbs="0.00"
    fi
    
    # 4. 使用 ip 收集网络统计信息
    interface=$(ip route get 1 | awk '{print $5; exit}')
    rx_bytes=$(ip -s link show $interface | awk '/RX:/{getline; print $1}')
    tx_bytes=$(ip -s link show $interface | awk '/TX:/{getline; print $1}')
    
    # 5. 解析并格式化数据为 JSON
    echo "{"
    echo "  \"timestamp\": $timestamp,"
    
    # CPU 信息
    echo "  \"cpu\": {"
    echo "    \"user\": $(echo "$top_data" | grep '%Cpu(s)' | awk '{print $2}'),"
    echo "    \"system\": $(echo "$top_data" | grep '%Cpu(s)' | awk '{print $4}'),"
    echo "    \"idle\": $(echo "$top_data" | grep '%Cpu(s)' | awk '{print $8}'),"
    echo "    \"load_avg\": \"$(echo "$top_data" | grep 'load average' | awk -F 'average: ' '{print $2}' | tr -d ',')\""
    echo "  },"
    
    # 进程信息
    echo "  \"processes\": {"
    echo "    \"total\": $(echo "$top_data" | grep 'Tasks' | awk '{print $2}'),"
    echo "    \"running\": $(echo "$top_data" | grep 'Tasks' | awk '{print $4}'),"
    echo "    \"sleeping\": $(echo "$top_data" | grep 'Tasks' | awk '{print $6}'),"
    echo "    \"stopped\": $(echo "$top_data" | grep 'Tasks' | awk '{print $8}'),"
    echo "    \"zombie\": $(echo "$top_data" | grep 'Tasks' | awk '{print $10}')"
    echo "  },"
    
    # 内存信息
    echo "  \"memory\": {"
    echo "    \"total\": $(echo $mem_data | awk '{print $2}'),"
    echo "    \"free\": $(echo $mem_data | awk '{print $3}'),"
    echo "    \"used\": $(echo $mem_data | awk '{print $4}'),"
    echo "    \"buff_cache\": $(echo $mem_data | awk '{print $6}'),"
    echo "    \"available\": $(echo $mem_data | awk '{print $7}')"
    echo "  },"
    
    # Swap 信息
    echo "  \"swap\": {"
    echo "    \"total\": $(echo $swap_data | awk '{print $2}'),"
    echo "    \"free\": $(echo $swap_data | awk '{print $3}'),"
    echo "    \"used\": $(echo $swap_data | awk '{print $4}')"
    echo "  },"
    
    # 磁盘 I/O 信息（使用 iotop 收集）
    echo "  \"disk_io\": {"
    echo "    \"read_kbs\": \"$read_kbs\","
    echo "    \"write_kbs\": \"$write_kbs\""
    echo "  },"
    
    # 网络统计信息
    echo "  \"network\": {"
    echo "    \"rx_bytes\": \"$rx_bytes\","
    echo "    \"tx_bytes\": \"$tx_bytes\""
    echo "  }"
    echo "}"
}

# 主执行函数
main() {
    collect_perf_data
}

main
